﻿namespace KeRui.Upload
{
    using System;
    using System.IO;
    using System.Text;

    internal class SuperStream : SubStream
    {
        public SuperStream(Stream inStream) : base(inStream)
        {
        }

        public SuperStream(SubStream inStream, int inStart, int inLength) : base(inStream, inStart, inLength)
        {
        }

        public bool EqualStr(byte[] inSearch)
        {
            bool flag;
            if (inSearch.Length > (this.Length - this.Position))
            {
                return false;
            }
            Stream inStream = base.Lock();
            try
            {
                flag = EqualStr(inSearch, inStream);
            }
            finally
            {
                base.Unlock();
            }
            return flag;
        }

        private static bool EqualStr(byte[] inSearch, Stream inStream)
        {
            bool flag = false;
            if ((inSearch != null) && (inSearch.Length >= 1))
            {
                long num = 0L;
                long position = inStream.Position;
                long num3 = inSearch.Length - 1;
                while (!flag)
                {
                    int num4 = inStream.ReadByte();
                    if ((num4 < 0) || (inSearch[(int) ((IntPtr) num)] != num4))
                    {
                        break;
                    }
                    num += 1L;
                    if (num >= num3)
                    {
                        flag = true;
                    }
                }
                inStream.Position = position;
            }
            return flag;
        }

        public long FindStr(byte[] inSearch)
        {
            if ((inSearch != null) && (inSearch.Length >= 1))
            {
                long num = (this.Length - this.Position) - inSearch.Length;
                int num2 = inSearch[0];
                Stream inStream = base.Lock();
                try
                {
                    int num3;
                    goto Label_00A8;
                Label_0038:
                    num3 = inStream.ReadByte();
                    if (num2 == num3)
                    {
                        long position = inStream.Position;
                        inStream.Position -= 1L;
                        if (EqualStr(inSearch, inStream))
                        {
                            this.Position = (inStream.Position - base.Start) + inSearch.Length;
                            return (this.Position - inSearch.Length);
                        }
                        inStream.Position = position;
                    }
                Label_00A8:
                    num -= 1L;
                    if (num >= 0L)
                    {
                        goto Label_0038;
                    }
                }
                finally
                {
                    base.Unlock();
                }
                this.Position = this.Length;
            }
            return -1L;
        }

        public byte GetBYTE()
        {
            return (byte) this.ReadByte();
        }

        public uint GetMacDWORD()
        {
            uint num = (uint) this.ReadByte();
            uint num2 = (uint) this.ReadByte();
            uint num3 = (uint) this.ReadByte();
            uint num4 = (uint) this.ReadByte();
            return ((((num << 0x18) | (num2 << 0x10)) | (num3 << 8)) | num4);
        }

        public int GetMacLONG()
        {
            int num = this.ReadByte();
            int num2 = this.ReadByte();
            int num3 = this.ReadByte();
            int num4 = this.ReadByte();
            return ((((num << 0x18) | (num2 << 0x10)) | (num3 << 8)) | num4);
        }

        public short GetMacSHORT()
        {
            int num = this.ReadByte();
            int num2 = this.ReadByte();
            return (short) ((num << 0x18) | (num2 << 0x10));
        }

        public ushort GetMacWORD()
        {
            uint num = (uint) this.ReadByte();
            uint num2 = (uint) this.ReadByte();
            return (ushort) ((num << 0x18) | (num2 << 0x10));
        }

        public uint GetOSType()
        {
            return this.GetMacDWORD();
        }

        public static byte[] OSTypeToBytes(uint inType)
        {
            return new byte[] { ((byte) ((inType & -16777216L) >> 0x18)), ((byte) ((inType & 0xff0000) >> 0x10)), ((byte) ((inType & 0xff00) >> 8)), ((byte) (inType & 0xff)) };
        }

        public static string OSTypeToString(uint inType)
        {
            return Encoding.ASCII.GetString(OSTypeToBytes(inType));
        }

        public static uint StringToOSType(string inStr)
        {
            Stream inStream = new MemoryStream(Encoding.ASCII.GetBytes(inStr));
            SuperStream stream2 = new SuperStream(inStream);
            return stream2.GetOSType();
        }
    }
}

